// pages/training/training.js
import Com from '../../JavaScript/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    setInter: '',
    // 耗时
    num: '00:00:00',
    // 题库
    question_bank: [
      { text: '12-5+3', value: 10, answer: '' },
      { text: '17+5', value: 22, answer: '' },
      { text: '8+2+3', value: 13, answer: '' },
      { text: '6-2+9', value: 13, answer: '' },
      { text: '5+3+9', value: 17, answer: '' },
      { text: '3+7+5', value: 15, answer: '' },
      { text: '7+6+5', value: 18, answer: '' },
      { text: '7+7+3', value: 17, answer: '' },
      { text: '6+8+2', value: 16, answer: '' },
      { text: '4+5+5', value: 14, answer: '' }
    ],
    // 当前题目
    index:0

  },
  nextQuestion(){
    // let arr = 'question_bank[' + this.data.index + '].answer'
    // this.setData({
    //   [arr]: this.data.question_bank[this.data.index].answer + e.currentTarget.dataset.value
    // })
    
    if (this.data.index + 1 == this.data.question_bank.length){
      let ids = Date.parse(new Date()) + Math.floor(Math.random() * 50000);
      let correct = 0, error = 0, score=0;
      for (let i = 0; i < this.data.question_bank.length;i++){
        if (this.data.question_bank[i].value == this.data.question_bank[i].answer){
          correct+=1;
        }else{
          error+=1;
        }
      }
      score = 100 / this.data.question_bank.length * correct;
      let arr = { id: ids, time: this.data.num, answer: this.data.question_bank, correct: correct, error: error, score: score.toFixed(0)};
      let storRecord = Com.getStor('answer_record')
      if (storRecord){
        storRecord.push(arr);
        Com.setStor('answer_record', storRecord)
      }else{
        Com.setStor('answer_record', [arr] )
      }
      wx.navigateTo({
        url: '../complete/complete?id='+ids
      })
    }else{
      this.setData({
        index: this.data.index + 1
      })
    }
    
  },
  onActive(e){
    let arr = 'question_bank[' + this.data.index +'].answer'
    if (this.data.question_bank[this.data.index].answer.length>5){
      return;
    }
    this.setData({
      [arr]: this.data.question_bank[this.data.index].answer + e.currentTarget.dataset.value
    })
  },
  onDeleteActive(){
    let arr = 'question_bank[' + this.data.index + '].answer'
    this.setData({
      [arr]: this.data.question_bank[this.data.index].answer.substring(0, this.data.question_bank[this.data.index].answer.length-1)
    })
  },
  startSetInter: function () {

    var that = this;
    that.data.setInter = setInterval(
      function () {
        that.countdown()
      }
      , 1000);

  },
  // 时间倒计时
  countdown() {

    var that = this;
    var s = '';
    var hour = that.data.num.split(':')[0];
    var min = that.data.num.split(':')[1];
    var sec = that.data.num.split(':')[2];

    s = Number(hour * 3600) + Number(min * 60) + Number(sec)+1;
    // var numVal = that.data.num - 1;
    // that.setData({ num: s+1 });

    var t;
    if (s > -1) {
      var hour = Math.floor(s / 3600);
      var min = Math.floor(s / 60) % 60;
      var sec = s % 60;
      if (hour < 10) {
        t = '0' + hour + ":";
      } else {
        t = hour + ":";
      }

      if (min < 10) { t += "0"; }
      t += min + ":";
      if (sec < 10) { t += "0"; }
      t += sec;
      that.setData({ num: t });
    }
  },
  
  send() {
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.startSetInter();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})